package com.jiangjiang.eshop.inventory.updater;

import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.domain.GoodsStockEntity;
import com.jiangjiang.eshop.inventory.service.InventoryGoodsStockService;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName ReturnInputStockFactory
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/7 19:28
 * @Version 1.0
 **/
@Component
public class ReturnInputStockFactory<T> extends AbstractStockUpdaterFactory<T> {
    /**
     * 构造方法
     *
     * @param goodsStockService
     * @param dateHelper
     */
    @Autowired
    public ReturnInputStockFactory(
            InventoryGoodsStockService goodsStockService,
            DateHelper dateHelper) {
        super(goodsStockService, dateHelper);
    }

    @Override
    protected List<Long> getGoodsSkuIds(T parameter) throws Exception {
        ReturnGoodsInputOrderDTO dto = (ReturnGoodsInputOrderDTO) parameter;
        return dto.getItems().stream().map(ReturnGoodsInputOrderItemDTO::getGoodSkuId).collect(Collectors.toList());
    }

    @Override
    protected StockUpdater createUpdater(List<GoodsStockEntity> goodsStock, T parameter) {
        ReturnGoodsInputOrderDTO dto = (ReturnGoodsInputOrderDTO) parameter;
        Map<Long, ReturnGoodsInputOrderItemDTO> itemDTOMap = dto.getItems()
                .stream()
                .collect(Collectors.toMap(ReturnGoodsInputOrderItemDTO::getGoodSkuId, Function.identity()));
        return new ReturnGoodsInputStockUpdater(goodsStockService,goodsStock,dateHelper,itemDTOMap);
    }
}
